<% content_for :admin_tools do %>
  <% if current_user.admin? || current_user.editor? %>
    <p><%= link_to "Create New Topic", new_topic_path %></p>
  <% end %>
<% end %>


<div id="manage-topics">
  
  <% if current_user.admin? || current_user.editor? %>
    <div class="content-section">
      <ul class="actions">
        <li>
          <% if @upcoming -%>
            Showing only topics with upcoming sessions. 
            <%= link_to "Show all topics.", manage_topics_path(topics: :all) %>
          <% else -%>
            Showing all topics.
            <%= link_to "Show only topics with upcoming sessions.", manage_topics_path(topics: :upcoming) %>
          <% end -%>
        </li>
        <% if current_user.admin? && current_user.editor? %>
          <li>
            <% if @all_depts -%>
              Showing topics in all departments.
              <%= link_to "Show only topics in your #{pluralize_word(current_user.departments.size, 'department')}.", manage_topics_path(departments: :user) %>
            <% else -%>
              Showing only topics in your <%= pluralize_word(current_user.departments.size, 'department') %>. 
              <%= link_to "Show all departments.", manage_topics_path(departments: :all) %>
            <% end -%>
          </li>
        <% end %>
      </ul>
    </div>
  <% end %>
  
  <% if @departments.blank? %>
    <div class="content-section">
      <h2>No topics found</h2>
      <div class="empty-list">
        <p>There are no topics to display.</p>
      </div>
    </div>
  <% else %>
  
    <% @departments.each do |department| %>
      <% authorized = authorized?(department) || current_user.editor?(department) %>
      <% cache(["#{date_slug}/topics/manage/#{@upcoming ? 'upcoming' : 'all'}", department, "#{authorized ? '' : 'not-'}authorized"], :tag => department.cache_key, expires_in: 1.day) do %>
        <div class="content-section">
          <h2><%= department.name %>
              <%= link_to("Subscribe to the XML feed for this department",
                department_path(department, :format => :atom), :class => 'feed-link') %>
          </h2>
          <div class="action-list">
            <% if authorized %>
              <p class="download-link download-csv">
                <%= link_to "Download Attendance History", department_path( department, :format => :csv ) %>
              </p>
            <% end %>
          </div>
          <% topics = @topics[department.id] %>
          <% if topics.blank? %>
            <div class="empty-list">
              <p>There are no topics <%= @upcoming ? ' with upcoming sessions ' : '' %> in this department.</p>
            </div>
          <% else %>
            <table class="topic-list">
              <thead>
                <tr>
                  <th scope="col" class="topic-name">Topic</th>
                  <th scope="col" class="topic-date">Next Session</th> 
                  <th scope="col" class="topic-count">Reservations</th>
                </tr>
              </thead>
              <tbody>
                <% topics.each do |topic| %>
                  <% cache(["#{date_slug}/topics/manage/#{@upcoming ? 'upcoming' : 'all'}", topic], :tag => topic.cache_key, expires_in: 1.day) do %>
                    <tr class="<%= cycle("even", "odd") %>">
                    <td class="topic-name">
                      <%= link_to topic.name, history_topic_path(topic) %>
                    </td>
                    <td class="topic-date">
                      <% if topic.upcoming_sessions.present? %>       
                        <%= link_to session_time_short(topic.upcoming_sessions[0]), topic.upcoming_sessions[0] %>
                      <% else %>
                        <div class="empty">-</div>
                      <% end %>
                    </td>
                    <td class="topic-count">
                      <% if topic.upcoming_sessions.present? %>       
                        <%= session_reservations_short(topic.upcoming_sessions[0]) %>
                      <% else %>
                       <div class="empty">-</div>
                      <% end %>
                    </tr>
                  <% end %>
                <% end %>
              </tbody>
            </table>
            <% reset_cycle %>
          <% end %>
        </div>
      <% end %>
    <% end %>
  <% end %>
</div>
